from collections import deque


def print_num_recursion(n):
    if n > 0:
        print_num_recursion(n - 1)
        print(n)


class Stack:
    def __init__(self):
        self._deque = deque()

    def push(self, value):
        self._deque.append(value)

    def pop(self):
        return self._deque.pop()

    def is_empty(self):
        return len(self._deque) == 0


def print_num_recursion1(n):
    s = Stack()
    while n > 0:
        s.push(n)
        n -= 1
    while not s.is_empty():
        print(s.pop())


# 汉诺塔
def move(n, source, dest, intermediate):  # 将n个盘子从source挪至dest
    if n >= 1:  # 杆杆上只有一个的时候就停止
        move(n - 1, source, intermediate, dest)  # n-1个盘子从source挪至intermdiate
        print("Move %s -> %s" % (source, dest))
        move(n - 1, intermediate, dest, source)  # 将n-1个盘子从inter挪至dest


class A:
    def __init__(self):
        self.l1 = []

    def flatten(self, l):
        for i in l:
            if isinstance(i, list):
                self.flatten(i)
            else:
                self.l1.append(i)


# 可以使用闭包解决
def flatten(l):
    l1 = []

    def f(l):
        for i in l:
            if isinstance(i, list):
                f(i)
            else:
                l1.append(i)
        return l1

    return f(l)


if __name__ == '__main__':
    # print_num_recursion(1000) # 调用这个递归方法会栈溢出
    # print_num_recursion1(1000)  # 使用这个栈不会溢出

    move(3, 'A', 'C', 'B')
    # a = A()
    # a.flatten([[1, 2], [1, 2, 3]])
    # print(a.l1)
    pass
    print(flatten([[1, 2], [1, 2, 3]]))
    print(flatten([[3, 2], [1, 2, 3]]))
